!(function(window, factory){

    if(typeof define == 'function' && define.amd){

        var jssdk = '//res.wx.qq.com/open/js/jweixin-1.0.0.js';
        define('wxshare', [ jssdk ], function( wx ){
            return {
                share: factory(window, wx),
                wx: wx
            };
        })
    }
    else{

        window.Zero = window.Zero || {};
        window.Zero.share = factory(window, wx);

        if(window._za_share_data_){
            // app 那边接口初始化相对有一点迟，我们这边需要延迟调用
            $(function(){
                window.Zero.share( window._za_share_data_, window._za_share_config_ );
            })
        }
    }

})(this, function(window, wx){

    var isInWechat = /MicroMessenger/i.test(window.navigator.userAgent);
    var onGetSignSuccess = function( data ) {

        wx.config({
            debug: window._za_share_debug_ === true, // 留一个开关来启用调试模式
            appId: data.appid,
            timestamp: data.timestamp,
            nonceStr: data.noncestr,
            signature: data.signature,
            jsApiList: 'checkJsApi;onMenuShareTimeline;onMenuShareAppMessage;onMenuShareQQ;onMenuShareWeibo;hideMenuItems;showMenuItems;hideAllNonBaseMenuItem;showAllNonBaseMenuItem;previewImage;chooseImage;uploadImage;downloadImage;startRecord;stopRecord;onVoiceRecordEnd;playVoice;stopVoice;onVoicePlayEnd;uploadVoice;downloadVoice;addCard;chooseCard;openCard;openLocation;getLocation'.split(';')
        });
    };

    //get token by appid
    isInWechat && $.ajax({
        type: 'POST',
        url: '/open/common/WeiXinScreen/getWeiXinSign.json',
        dataType: 'json',
        data:{
            url: location.href
        },
        success: onGetSignSuccess
    });

    return function share( config, za_config ) {
        // alert('app share set');

        var isInApp = /ZhongAnWebView/i.test(window.navigator.userAgent);
        var za_config = za_config || {};

        if(isInApp){

            var timer, setShareData = function(){

              // alert('this is setShareDate function inner')

                if(window.ZAIAppJSInterface){

                  // alert('this is setShareDate function inner , if has window.ZAIAppJSInterface');

                    if(timer){
                        clearInterval(timer);
                    }

                    var methodName = 'setAppLocalShareData',
                        cbName = '_za_share_callback_';

                    if( za_config.mode == 'pop' ){
                        methodName = 'appLocalShare';
                    }

                    // alert(za_config);

                    window[cbName] = function(ret){

                        // alert('ret；' + ret);

                        // alert('app share success callback');

                        if( !ret ) return;

                        if(ret.result == 1){
                            config.success && config.success(ret);
                        }
                        else if(ret.result == 0){
                            config.cancel && config.cancel(ret)
                        }
                    }

                    // alert(window[cbName]);

                    window.ZAIAppJSInterface[methodName]( za_config.type || '1', config.link, config.imgUrl, config.title, config.desc, cbName);
                }
            }

            if(window.ZAIAppJSInterface){
                // alert('setAppShareData');
                setShareData();
            }
            else{
                // alert('no setAppShareData, set timer');
                timer = setInterval(setShareData, 200);
            }

            return;
        }

        var shareData = {
            imgUrl: config.imgUrl,
            link: config.link,
            desc: config.desc,
            title: config.title,
            success: config.success,
            cancel: config.cancel
        }, timeLineData = $.extend({},shareData);

        if(config.richTitle){
            // 分享到朋友圈的title要更丰富一些
            timeLineData.title = config.richTitle
        }

        wx.ready(function() {
            wx.onMenuShareAppMessage(shareData);
            wx.onMenuShareTimeline(timeLineData);
            wx.onMenuShareQQ(shareData);
            wx.onMenuShareWeibo(shareData);
        })
    }

});
